1 00:00:00,480 --> 00:00:00,990 Hey there. 2 00:00:00,990 --> 00:00:01,860 Welcome back. 3 00:00:01,860 --> 00:00:07,320 In this lecture we're going to be taking a quick look at what an enum or enumerated type is. 4 00:00:07,590 --> 00:00:12,840 An enum is simply a data type that consists of a set of constant values. 5 00:00:12,840 --> 00:00:18,840 These constants are used in conjunction with other APIs to define presets for specific things. 6 00:00:18,840 --> 00:00:25,530 In other words, an enum is simply a container of constant values with meaningful names to make it easier 7 00:00:25,530 --> 00:00:27,030 for us to code. 8 00:00:27,700 --> 00:00:32,800 Imagine if we wanted to have a set of ten possible player states for a player in our game, and yet 9 00:00:32,800 --> 00:00:36,910 we never defined a constant value to represent each one of those ten player states. 10 00:00:36,910 --> 00:00:38,980 That would be a total mess. 11 00:00:39,250 --> 00:00:44,680 However, we could have an enum to represent the state of a player when they're walking or jumping or 12 00:00:44,680 --> 00:00:45,550 falling over. 13 00:00:45,580 --> 00:00:51,130 However, if we didn't have that enum, we would have no idea what value to set on a player. 14 00:00:51,130 --> 00:00:56,590 To give us that particular state, we first have to define that constant value to give us that state, 15 00:00:56,590 --> 00:01:02,290 and then we can bundle all those values together, give them each meaningful names like jumping or falling 16 00:01:02,290 --> 00:01:04,960 over, and bam, you've got yourself an enum. 17 00:01:05,320 --> 00:01:10,570 I'm going to create a script real quick and server script service to demonstrate what an enum is, and 18 00:01:10,570 --> 00:01:15,790 you should know to access all of the enums in the Roblox API, you just type out the keyword of enum, 19 00:01:15,790 --> 00:01:17,080 and then you can index it. 20 00:01:17,080 --> 00:01:21,640 And here it will show you all of the different enums that exist in the game. 21 00:01:21,640 --> 00:01:27,310 And these enums are used for many different things, and they help us interact with the many services 22 00:01:27,310 --> 00:01:30,970 and API functions that we have in the Roblox API. 23 00:01:32,100 --> 00:01:35,520 For example, one of these enums is called a part type. 24 00:01:35,520 --> 00:01:41,220 And if we index the part type enum here, it shows us a set of constants that we can go ahead and apply 25 00:01:41,220 --> 00:01:44,610 to a part in our game to change its shape. 26 00:01:44,610 --> 00:01:48,360 So let's go ahead and demonstrate that I'm going to create a new part. 27 00:01:50,060 --> 00:01:54,590 And I'm going to position my part above the origin in the workspace. 28 00:01:54,590 --> 00:01:56,510 So that'll be equal to vector three dot new. 29 00:01:56,510 --> 00:01:59,540 And we'll just put it ten studs above the origin point. 30 00:01:59,990 --> 00:02:01,970 We'll make sure this part is anchored. 31 00:02:01,970 --> 00:02:04,820 And then I'm going to set its parent equal to the workspace. 32 00:02:04,820 --> 00:02:09,080 And when we set it into the workspace you'll see it's just a regular part or a block. 33 00:02:09,080 --> 00:02:12,320 But then I'm going to yield here for five seconds. 34 00:02:12,320 --> 00:02:15,170 And then I'm going to update the shape property. 35 00:02:15,170 --> 00:02:18,260 And here it tells us it takes an enum of part type. 36 00:02:18,260 --> 00:02:21,470 And we can go ahead and change the shape of our part. 37 00:02:21,470 --> 00:02:24,710 For example we could change it to the part type of ball. 38 00:02:24,890 --> 00:02:26,870 And then I'm going to wait for another five seconds. 39 00:02:26,870 --> 00:02:32,810 And then we can change the shape maybe to an enum part type of let's say cylinder. 40 00:02:32,870 --> 00:02:39,350 We can wait another five seconds and then we can change the shape maybe to an enum part type of let's 41 00:02:39,350 --> 00:02:40,550 say wedge. 42 00:02:41,700 --> 00:02:47,820 Each of these represents a constant value, and these are just the names that Roblox has given to them. 43 00:02:47,820 --> 00:02:52,620 So one value, they've called it ball, which will set the shape of the part equal to ball. 44 00:02:52,650 --> 00:02:57,090 Another one they called it cylinder which will set the shape equal to a cylinder, and so on. 45 00:02:57,750 --> 00:03:03,300 So an enum is simply just a set of constant values with meaningful names. 46 00:03:03,840 --> 00:03:06,510 So if we go and run our game here. 47 00:03:08,010 --> 00:03:13,140 As you can see here is our regular brick and then it should change into a sphere. 48 00:03:13,170 --> 00:03:13,920 There you go. 49 00:03:13,920 --> 00:03:15,810 There's our little sphere right there. 50 00:03:15,810 --> 00:03:18,780 And then it should change into a cylinder. 51 00:03:18,780 --> 00:03:22,800 There's our cylinder and then it should change into a wedge. 52 00:03:23,400 --> 00:03:23,880 Perfect. 53 00:03:23,880 --> 00:03:24,390 There we go. 54 00:03:24,390 --> 00:03:25,980 Here is our wedge. 55 00:03:27,610 --> 00:03:32,710 Earlier I mentioned talking about having an enum for the different states a player could be in, and 56 00:03:32,710 --> 00:03:37,900 that actually exists within the Roblox API specifically for the humanoid instance. 57 00:03:37,900 --> 00:03:43,450 So every player's character in the game is going to have a humanoid, and I'm just going to create one 58 00:03:43,450 --> 00:03:44,170 here real quick. 59 00:03:44,170 --> 00:03:48,700 I'll create a new humanoid, and these humanoids have a function inside of them where we are able to 60 00:03:48,700 --> 00:03:54,580 change the state of the humanoid, and it takes an enum of humanoid state type. 61 00:03:55,090 --> 00:04:01,540 So if we access the humanoid state type enum and we index it here, it shows us all the different constants 62 00:04:01,540 --> 00:04:05,320 or possible values that a humanoid could be in. 63 00:04:05,320 --> 00:04:10,240 For example, they could be in a dead state, they could be in a state where they're falling down or 64 00:04:10,240 --> 00:04:11,200 they're jumping. 65 00:04:11,200 --> 00:04:13,390 Maybe they're running or they're seated. 66 00:04:13,390 --> 00:04:17,470 They could be in a state of free falling or they're swimming, stuff like that. 67 00:04:17,470 --> 00:04:22,180 So there's a whole bunch of different constants here to define the different states that a humanoid 68 00:04:22,180 --> 00:04:22,900 could be in. 69 00:04:22,900 --> 00:04:29,140 We also have another enum when it relates to user input for example, how do we know what particular 70 00:04:29,140 --> 00:04:31,720 key a player is pressing on their keyboard? 71 00:04:31,720 --> 00:04:37,540 Well, thankfully we have an enum called Keycode, which stores all of the different keys that a player 72 00:04:37,540 --> 00:04:39,580 could possibly press on their keyboard. 73 00:04:39,580 --> 00:04:45,160 So that way when a player does press a keyboard, it'll tell us what enum Keycode it was, and then 74 00:04:45,160 --> 00:04:49,300 we can go ahead and compare that key code to another key code. 75 00:04:49,300 --> 00:04:55,600 And then if it matches, then we can execute some functionality based on what key a player presses on 76 00:04:55,600 --> 00:04:56,590 their keyboard. 77 00:04:56,920 --> 00:05:00,190 We also have another enum for storing specific fonts in our game. 78 00:05:00,190 --> 00:05:01,630 So this is called font. 79 00:05:01,750 --> 00:05:06,160 And here are a bunch of different fonts that we can set for stuff in our game. 80 00:05:06,160 --> 00:05:09,040 We also have an enum for font sizes. 81 00:05:09,040 --> 00:05:11,080 So if we access that there, boom! 82 00:05:11,080 --> 00:05:16,930 We have a whole bunch of standardized font sizes where if we have different font styles, different 83 00:05:16,930 --> 00:05:21,700 font weights, there is just a whole bunch of different enums in the Roblox API. 84 00:05:22,190 --> 00:05:27,860 So as I stated, an enum is just a set of standardized values to enable us to communicate between the 85 00:05:27,860 --> 00:05:30,500 different functions and services in our game. 86 00:05:30,500 --> 00:05:36,770 If no enums existed, we would have no idea what values to set in properties or passed to functions 87 00:05:36,770 --> 00:05:38,390 to get the state we want. 88 00:05:38,390 --> 00:05:40,370 That's all I have for you in this lecture. 89 00:05:40,370 --> 00:05:43,910 Enums are quite simple and I'll see you in the next one.